﻿
using System;

namespace EventBusNet6.Controllers
{
    [Route("api")]
    [ApiController]
    public class GhostscirptController : ControllerBase
    {
        private readonly Serilog.ILogger _logger = Log.Logger;
        private readonly IGhostscirptService _ghostscirptService;
        public GhostscirptController(IGhostscirptService ghostscirptService)
        {
            _ghostscirptService = ghostscirptService;
        }
        [HttpPost("pdf2png")]
        public ApiResult Pdf2Png(Pdf2PngInput input)
        {
            Pdf2PngOutput  output  =new Pdf2PngOutput ();
            var guid = Guid.NewGuid().ToString();
            output.Guid = guid;
            output.CatalogFile= _ghostscirptService.ConvertPDF2PNG(input.PDFPath, guid);
            return FormatterResult.Success(output);
        }

        [HttpPost("pdf2jpeg")]
        public ApiResult Pdf2Jpeg(Pdf2JpegInput input)
        {
            Pdf2JpegOutput output = new Pdf2JpegOutput();
            var guid = Guid.NewGuid().ToString();
            output.Guid = guid;
            output.CatalogFile = _ghostscirptService.ConvertPDF2JPEG(input.PDFPath, guid);
            return FormatterResult.Success(output);
        }

        [HttpPost("pdf2pngoptpage")]
        public ApiResult Pdf2PngOptPage(Pdf2PngOptPageInput input)
        {
            Pdf2PngOptPageOutput output = new Pdf2PngOptPageOutput();
            var guid = Guid.NewGuid().ToString();
            output.Guid = guid;
            output.CatalogFile = _ghostscirptService.Pdf2PngOptPage(input.PDFPath, guid, input.FirstPage, input.LastPage);
            return FormatterResult.Success(output);
        }

        /// <summary>
        /// pdf 压缩
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("pdfcompress")]
        public ApiResult PdfCompression(PdfCompressionInput input)
        {
            PdfCompressionOutput output = new PdfCompressionOutput();
            var guid = Guid.NewGuid().ToString();
            output.Guid = guid;
            output.CatalogFile = _ghostscirptService.PdfCompression(input.PDFPath, guid);
            return FormatterResult.Success(output);
        }
        /// <summary>
        /// pdf 拆分输入SplitFirstPage-SplitLastPage 为一个新的pdf
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("pdfsplit")]
        public ApiResult PdfSplit(PdfSplitInput input)
        {
            PdfSplitOutput output = new PdfSplitOutput();
            var guid = Guid.NewGuid().ToString();
            output.Guid = guid;
            output.CatalogFile = _ghostscirptService.PdfSplit(input.PDFPath, guid, input.SplitFirstPage,input.SplitLastPage);
            return FormatterResult.Success(output);
        }
        /// <summary>
        /// pdf 一个或者多个pdf合并
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("pdfcombine")]
        public ApiResult PdfCombined(PdfCombinedInput input)
        {
            if (input.PDFPaths.Count==0)
            {
                return FormatterResult.Failed("pdf输入参数错误", 500);
            }
            PdfCompressionOutput output = new PdfCompressionOutput();
            var guid = Guid.NewGuid().ToString();
            output.Guid = guid;
            output.CatalogFile = _ghostscirptService.PdfCombined(string.Join(" ",input.PDFPaths), guid);
            return FormatterResult.Success(output);
        }
    }
}
